package com.liliudong.keelflower.common.core.utils;

import com.liliudong.keelflower.common.core.constants.HttpStatus;
import lombok.Getter;

/**
 * 全局返回类
 *
 * @author Liudong.li
 * @param <T>
 */
@Getter
public class R<T> {

    private static final String DEFAULT_SUCCESS_MSG = "success";
    private static final String DEFAULT_FAIL_MSG = "fail";

    private final T data;
    private final int code;
    private final String msg;

    private R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> R<T> success(int code, String msg, T data) {
        return new R<>(code, msg, data);
    }

    public static <T> R<T> success(T data) {
        return success(0, DEFAULT_SUCCESS_MSG, data);
    }

    public static <T> R<T> success() {
        return success(HttpStatus.SUCCESS);
    }

    public static <T> R<T> fail(int code, String msg, T data) {
        return new R<>(code, msg, data);
    }

    public static <T> R<T> fail(int code, String msg) {
        return new R<>(code, msg, null);
    }

    public static <T> R<T> fail(String msg) {
        return fail(-1, msg);
    }

    public static <T> R<T> fail(int code) {
        return fail(code, DEFAULT_FAIL_MSG);
    }

    public static <T> R<T> fail() {
        return fail(-1);
    }

    public static <T> R<T> success(HttpStatus httpStatus) {
        return success(httpStatus.code, httpStatus.msg, null);
    }

    public static <T> R<T> success(HttpStatus httpStatus, T data) {
        return success(httpStatus.code, httpStatus.msg, data);
    }

    public static <T> R<T> fail(HttpStatus httpStatus) {
        return fail(httpStatus.code, httpStatus.msg);
    }


}
